// pages/addliuliangchi/addliuliangchi.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    columns: ['抖音','快手','淘宝','火山','小红书','咸鱼','微视','飞猪'],
    show: false,
    inputList: [
      {
        name: '抖音',
        value: ''
      }
    ],
    title: '',
    type: 0,
    isPageType: 1
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      type: options.type,
      // isPageType: options.isPageType
    })
  },
  titleChange(e){
    // console.log(e.detail.value,'titleChange')
    this.data.title = e.detail.value
  },
  openPop(){
    this.setData({
      show: true
    })
  },
  onConfirm(e){
    // console.log(e.detail.value,'onConfirm')
    let value = e.detail.value
    this.setData({
      show: false
    })
    let foundItem = this.data.inputList.find(item => item.name === value); 
    if(foundItem){
      wx.showToast({
        title: '场景重复',
        icon:'error'
      })
    }else {
      this.data.inputList.push({
        name: e.detail.value,
        value: ''
      })
      this.setData({
        inputList:this.data.inputList
      })
    }
    
    
  },
  onCancel(e){
    // console.log(e,'onCancel')
    this.setData({
      show: false
    })
  },
  onChange(e){
    // console.log(e,'onChange')
    // this.setData({
    //   show: false
    // })
  },
  onInputChange(e){
    // console.log(e.detail.value,e.currentTarget.dataset.index,'onInputChange')
    let index = e.currentTarget.dataset.index
    this.data.inputList[index].value = e.detail.value
    // console.log(this.data.inputList,'this.data.inputList')
    this.setData({
      inputList:this.data.inputList
    })

  },
  delKouling(e){
    // console.log(e,e.currentTarget.dataset.index,'delKouling')
    let index = e.currentTarget.dataset.index
    this.data.inputList.splice(index,1)
    this.setData({
      inputList:this.data.inputList
    })
  },
  addLiu(){

    // console.log(this.data.inputList,'ldldldldl',)
    const now = new Date();  
    const year = now.getFullYear();  
    const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始，所以要加1  
    const date = String(now.getDate()).padStart(2, '0');  
    const hours = String(now.getHours()).padStart(2, '0');  
    const minutes = String(now.getMinutes()).padStart(2, '0');  
    const seconds = String(now.getSeconds()).padStart(2, '0');  
    const currentTime = `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`;   
    // console.log(currentTime,'currentTime',this.data.title,this.data.inputList)
    let arr = wx.getStorageSync('liuList') ? wx.getStorageSync('liuList') : []
    arr.push({
      title: this.data.title,
      time: currentTime,
      koulingList: this.data.inputList
    })
    // console.log(arr,'arrrr')
    wx.setStorageSync('liuList', arr)
    // wx.redirectTo({
    //   url: '/pages/liuliangchiList/liuliangchiList?type=' + this.data.type + '&isPageType=' + this.data.isPageType,
    // })
    
    if (arr.length) {

      let pages = getCurrentPages();
      // prevPage 是获取上个页面的js里面的pages的所有信息，-2是上个页面，-3是上上个页面 以此类推
      let prevPage = pages[pages.length - 2];
      // 在本页面修改上一个页面的 data
      prevPage.setData({
        list: arr,

      })
      wx.navigateBack({
        delta: 1 //返回上一级页面
      })
      
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})